package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageBean;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.dto.NursingLevelPageQueryDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NursingLevelServiceImpl implements NursingLevelService {

    @Autowired
    private NursingLevelMapper nursingLevelMapper;
    @Autowired
    NursingPlanMapper nursingPlanMapper;

    /**
     * 分页条件查询护理等级
     *
     * @param dto
     * @return
     */
    @Override
    public PageBean<NursingLevelVo> pageQuery(NursingLevelPageQueryDto dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());

        Page<NursingLevelVo> page = nursingLevelMapper.pageQuery(dto);

        return PageBean.of(page , NursingLevelVo.class);
    }

    /**
     * 新增护理等级
     *
     * @param dto
     */
    @Override
    public void addNursingLevel(NursingLevelDto dto) {
        NursingLevel nursingLevel = BeanUtil.toBean(dto, NursingLevel.class);
        nursingLevel.setCreateTime(LocalDateTime.now());
        nursingLevel.setUpdateTime(LocalDateTime.now());
        nursingLevel.setCreateBy(1671403256519078239L);
        nursingLevel.setUpdateBy(1671403256519078239L);
        nursingLevel.setStatus(1);


        nursingLevelMapper.insert(nursingLevel);
    }

    /**
     * 根据ID查询护理等级
     *
     * @param id
     * @return
     */
    @Override
    public NursingLevelVo getById(Long id) {

        NursingLevel nursingLevel = nursingLevelMapper.getById(id);
        NursingLevelVo nursingLevelVo = BeanUtil.toBean(nursingLevel, NursingLevelVo.class);

        return nursingLevelVo;
    }

    /**
     * 修改护理等级
     *
     * @param dto
     */
    @Override
    public void updateNursingLevel(NursingLevelDto dto) {
//        1.先拿到之前的对象看，取出创建时间和创建人
        NursingLevel nursingLevel = nursingLevelMapper.getById(dto.getId());
        Long createBy = nursingLevel.getCreateBy();
        LocalDateTime createTime = nursingLevel.getCreateTime();

//        2.删除该对象
        nursingLevelMapper.deleteById(dto.getId());

//        3. 再插入新的对象
        NursingLevel newNursingLevel = BeanUtil.toBean(dto, NursingLevel.class);
        newNursingLevel.setCreateTime(createTime);
        newNursingLevel.setCreateBy(createBy);
        newNursingLevel.setUpdateTime(LocalDateTime.now());
        newNursingLevel.setUpdateBy(1671403256519078239L);
        nursingLevelMapper.insert(newNursingLevel);


//        nursingLevelMapper.deleteById(dto.getId());
//
////        2.再新增护理等级
//        NursingLevel nursingLevel = BeanUtil.toBean(dto, NursingLevel.class);
//        nursingLevel.setCreateTime(LocalDateTime.now());
//        nursingLevel.setUpdateTime(LocalDateTime.now());
//        nursingLevel.setCreateBy(1671403256519078239L);
//        nursingLevel.setUpdateBy(1671403256519078239L);
//        nursingLevelMapper.insert(nursingLevel);

    }

    /**
     * 删除护理等级
     *
     * @param id
     */
    @Override
    public void deleteNursingLevel(Long id) {
        NursingLevel nursingLevel = nursingLevelMapper.getById(id);
        if(nursingLevel.getStatus()==1){
            throw new BaseException(BasicEnum.ENABLED_CANNOT_DELETED);
        }
        if(nursingLevel.getStatus()==null){
            throw new RuntimeException("查询不到该数据，请求数据非法");
        }

        nursingLevelMapper.deleteById(id);
    }

    /**
     * 修改护理等级的状态
     *
     * @param id
     * @param status
     */
    @Override
    public void update(Long id, Integer status) {
        NursingLevel nursingLevel = nursingLevelMapper.getById(id);
        nursingLevel.setStatus(status);

        nursingLevelMapper.update(nursingLevel);
    }

    /**
     * 获取所有护理等级
     *
     * @return
     */
    @Override
    public List<NursingLevelVo> list() {
        List<NursingLevel> nursingLevels = nursingLevelMapper.list();
        List<NursingLevelVo> nursingLevelVos = BeanUtil.copyToList(nursingLevels, NursingLevelVo.class);

        return nursingLevelVos;
    }

}


